package minov.test;

import java.util.Arrays;

import minov.parser.ArgumentDefinitionException;
import minov.parser.ArgumentParser;
import minov.parser.IntegerArgument;
import minov.parser.SwitchArgument;
import minov.parser.ValidationResult;

public class ParserReusing {

   public ParserReusing() {
      initialize();
   }

   ArgumentParser parser = new ArgumentParser();
   SwitchArgument version =  new SwitchArgument("v", "version");
   IntegerArgument logLevel = new IntegerArgument("l", "log", 0, 5, 0);

   private void initialize() {
      // add switch argument
      parser.addArgument("version", version);
      // add integer argument with boundaries 0-5 and default valie 0
      parser.addArgument("loglevel", logLevel);
   }

   private void print() {
      if (version.isEntered()) {
         System.out.println("Application version : 1.0.1");
      }
      if (logLevel.isEntered()) {
         System.out.println("Log level: " + logLevel.getValue());
      }
   }

   public void parseArguments(String[] arguments) {

      printTestsSeparator(arguments);

      // result of parsing, if all arguments are valid
      ValidationResult result = null;
      try {

         result = parser.parseArguments(arguments);

         if (result.isValid() == false) {
            System.out.println(result.errorMessage());
         }
         print();

      } catch (ArgumentDefinitionException exception) {
         System.out.println("Exception: " + exception.getMessage());
         return;
      }

   }

   private static void printTestsSeparator(String[] arguments) {
      System.out.println("=========================");
      System.out.println("Parsing arguments: " + Arrays.toString(arguments));
   }

   public static void main(String[] arguments) {

      ParserReusing resuser = new ParserReusing();
      String[] simpleArguments = {};
      resuser.parseArguments(simpleArguments);

      String[] simpleArguments2 = { "-v" };
      resuser.parseArguments(simpleArguments2);

      String[] simpleArguments3 = { "-l=1" };
      resuser.parseArguments(simpleArguments3);

      String[] simpleArguments4 = { "-l", "3" };
      resuser.parseArguments(simpleArguments4);

      String[] simpleArguments5 = { "X" };
      resuser.parseArguments(simpleArguments5);

      String[] simpleArguments6 = { "-l", "8" };
      resuser.parseArguments(simpleArguments6);
   }
}
